const request = require('request');
const cheerio = require('cheerio');
const fs = require('fs')

// const url = 'https://www.caifuzhongwen.com/fortune500/paiming/china500/2021_%e4%b8%ad%e5%9b%bd500%e5%bc%ba.htm'
const url = 'https://www.caifuzhongwen.com/fortune500/paiming/global500/2021_%e4%b8%96%e7%95%8c500%e5%bc%ba.htm'

request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    const $ = cheerio.load(body);
    const totalData = []
    
    $('#ui-table2').find('tbody').find('tr').each(function (index, value){
      // 向数组中存放数据
      totalData.push({
        name: $(value).find('a').text(),
      })
    })
    writeFs(totalData)
  }
});

function writeFs(totalData){
  fs.writeFile('./world500/2021.json', JSON.stringify(totalData), function (err, data) {
    if (err) {
      throw err
    }
    console.log('数据保存成功');
  })
}
